﻿#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nHydrate.DataImport
{
	public class Field : DatabaseBaseObject
	{
		private int _length = 50;

		public Field()
		{
			this.DataType = System.Data.SqlDbType.VarChar;
			this.IsBrowsable = true;
			this.Collate = string.Empty;
			this.DefaultValue = string.Empty;
		}

		public System.Data.SqlDbType DataType { get; set; }
		public string Collate { get; set; }
		public bool Identity { get; set; }
		public string DefaultValue { get; set; }
		public bool PrimaryKey { get; set; }
		public bool Nullable { get; set; }
		public int Scale { get; set; }
		public bool IsReadOnly { get; set; }
		public bool IsBrowsable { get; set; }

		private bool _isUnique;
		public bool IsUnique
		{
			get { return _isUnique || this.PrimaryKey; }
			set { _isUnique = value; }
		}

		private bool _isIndexed;
		public bool IsIndexed
		{
			get { return _isIndexed || this.PrimaryKey; }
			set { _isIndexed = value; }
		}

		public int SortOrder { get; set; }
		public bool IsComputed { get; set; }
		public string Formula { get; set; }

		public int Length
		{
			get { return _length; }
			set
			{
				if (value < 0) value = 0;
				_length = value;
			}
		}

		public override string ObjectType
		{
			get { return "Field"; }
		}

		public override string ToString()
		{
			return this.Name;
		}

		public string CorePropertiesHash
		{
			get
			{
				var prehash =
					this.Name + "|" +
					this.Identity + "|" +
					this.Nullable + "|" +
					this.DefaultValue + "|" +
					this.Length + "|" +
					this.IsComputed + "|" +
					this.Scale + "|" +
					this.Collate + "|" +
					this.PrimaryKey + "|" +
					this.IsBrowsable + "|" +
					this.DataType.ToString();
				//return HashHelper.Hash(prehash);
				return prehash;
			}
		}

		public override bool Equals(object obj)
		{
			var o = obj as Field;
			if (o == null)
				return false;

			if (this.Name != o.Name) return false;
			if (this.IsIndexed != o.IsIndexed) return false;
			if (this.IsUnique != o.IsUnique) return false;
			if (this.Collate != o.Collate) return false;
			if (this.DefaultValue != o.DefaultValue) return false;
			if (this.IsComputed != o.IsComputed) return false;
			if (this.Identity != o.Identity) return false;
			if (this.PrimaryKey != o.PrimaryKey) return false;
			if (this.Nullable != o.Nullable) return false;
			if (this.Length != o.Length) return false;
			if (this.Scale != o.Scale) return false;
			if (this.DataType != o.DataType) return false;

			return true;

		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

	}
}
